package cn.sym.pet.service.impl;

import cn.sym.basic.service.impl.BaseServiceImpl;
import cn.sym.basic.util.BaiduAuditUtils;
import cn.sym.org.domain.Employee;
import cn.sym.pet.domain.Pet;
import cn.sym.pet.domain.PetDetail;
import cn.sym.pet.domain.PetOnlineAuditLog;
import cn.sym.pet.mapper.PetDetailMapper;
import cn.sym.pet.mapper.PetMapper;
import cn.sym.pet.mapper.PetOnlineAuditLogMapper;
import cn.sym.pet.mapper.PetTypeMapper;
import cn.sym.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sym
 * @since 2022-02-24
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void addOrUpdate(Pet pet, Employee employee) {
        if (pet.getId() == null) {
            initPet(pet, employee.getShop_id());
            //设置默认状态为0 下架状态
            pet.setState(0L);
            //1.保存宠物信息
            petMapper.add(pet);
            //2.保存宠物详情信息
            PetDetail detail = pet.getDetail();
            if (detail != null) {
                detail.setPetId(pet.getId());
                petDetailMapper.add(detail);
            }
        } else {
            PetDetail petDetail = initPetDetail(pet);
            System.out.println(petDetail);
            petDetailMapper.update(petDetail);
        }


    }

    private PetDetail initPetDetail(Pet pet) {
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        detail.setIntro(detail.getIntro());
        detail.setAdoptNotice(detail.getAdoptNotice());
        return detail;
    }

    /**
     * 初始化pet方法
     * @param pet
     * @param shopId
     */
    private void initPet(Pet pet, Long shopId) {
        pet.setShopId(shopId);
    }


    /**
     * 宠物批量上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        //先审核需要上架的宠物信息是否合法
        for (Long id : ids) {
            //通过id去查询出宠物信息
            Pet pet = petMapper.loadById(id);
            StringBuilder node = new StringBuilder();
            //如果宠物名字不合法
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                node.append("宠物名字不合法");
            }
            //对上传的资源图片做审核
           /* String resources = pet.getResources();
            if (StringUtils.isEmpty(resources)){
                String[] split = resources.split(",");
                if (!BaiduAuditUtils.ImgCensor("http://47.95.117.210:8888"+split[0])){
                    node.append("宠物名字不合法");
                }
            }*/
            //审核失败，记录审核失败的日志信息
            if (node.length()>0){
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setNote(node.toString());
                petOnlineAuditLogMapper.add(petOnlineAuditLog);

                //合法-调整状态、上架时间，保存审核日志
            }else {
                Map<String, Object> map = new HashMap<>();
                map.put("id",id);//要对这些宠物下架
                map.put("onSaleTime", new Date());
                petMapper.onSale(map);
            }
        }
    }

    /**
     * 宠物批量下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);//要对这些宠物下架
        map.put("offSaleTime", new Date());
        petMapper.offSale(map);

    }

    @Override
    @Transactional
    public void remove(Long id) {
        PetDetail petDetail = petDetailMapper.loadByPetId(id);
        petDetailMapper.delete(petDetail.getId());
        super.remove(id);
    }

}
